package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.impl.BrandServiceImpl;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理品牌相关请求的控制器
 *
 * @author Tan
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/brands")
@Api(tags = "02. 品牌管理模块")
public class BrandController {
    @Autowired
    BrandServiceImpl service;

    /**
     * 向数据库中添加品牌
     *
     * @param dto 要添加的品牌的dto对象
     * @return 添加状态
     */
    //  http://localhost:8080/brands/add-new?name=百事可乐&pinyin=kekoukele&logo=11111&description=111&keywords=可乐&sort=1&sales=100&productCount=100&commentCount=100&positiveCommentCount=100&enable=100
    @PostMapping("/add-new")
    @ApiOperation(value = "添加品牌")
    @ApiOperationSupport(order = 101)
    public JsonResult<Void> addNew(BrandAddNewDTO dto) {
        log.debug("开始处理[添加品牌]请求,参数{}", dto);
        service.addNew(dto);
        log.debug("品牌添加成功");
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation(value = "删除品牌")
    @ApiOperationSupport(order = 201)
    public JsonResult delete(@PathVariable Long id){
        log.debug("开始处理删除id为{}的品牌的请求",id);
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation(value = "查询品牌列表")
    @ApiOperationSupport(order = 401)
    public JsonResult list (){
        log.debug("开始处理查询品牌列表的请求,无参数");
        List<BrandListItemVO> list = service.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{id}/getStandById")
    @ApiOperation(value = "查询商品详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<BrandStandardVO> getStandById(@PathVariable Long id) {
        log.debug("开始处理[查询商品详情]的请求,参数:{}", id);
        BrandStandardVO vo = service.getStandardById(id);
        return JsonResult.ok(vo);
    }

    @PostMapping("/{id}/enable")
    @ApiOperation(value = "启用品牌")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> enable(@PathVariable Long id){
        log.debug("开始处理启用品牌的请求,参数:{}",id);
        service.enable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/unEnable")
    @ApiOperation(value = "禁用品牌")
    @ApiOperationSupport(order = 310)
    public JsonResult<Void> unEnable(@PathVariable Long id){
        log.debug("开始处理启用品牌的请求,参数:{}",id);
        service.unEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id修改品牌信息")
    @ApiOperationSupport(order = 320)
    public JsonResult<Void> updateById(BrandUpdateDTO brandUpdateDTO){
        log.debug("开始处理修改品牌信息的请求,参数:{}",brandUpdateDTO);
        service.updateById(brandUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation(value = "重建品牌缓存")
    @ApiOperationSupport(order = 610)
    @PostMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache(){
        log.debug("开始处理重建缓存的请求,无参数");
        service.rebuildCache();
        return JsonResult.ok();
    }
}
